{% extends '../layout.html'%}

{% block content %}
<body style="padding-bottom: 49px;">
<header class="main-header flex-wrap">
    <a href="javascript:history.back();" class="back-btn left">
        <img src="./img/back_icon.png" width="10" height="17" alt="">
    </a>
    <div class="flex-1">选择支付方式</div>
</header>

<div class="goods-total">
    商品名称
</div>
<ul class="confirm-list">
    <li class="flex-wrap">
        <div class="flex-1 ellipsis">{{product.ProductName}}{{product.ProductSubName}}</div>
    </li>
    <li class="flex-wrap">
        <div class="flex-1">
            数量:{{num}}件
        </div>
    </li>
</ul>

<div class="goods-total flex-wrap" >
    <div class="flex-1">使用积分 &nbsp;&nbsp; 可使用积分{{userInfo.Score}}</div>
    <label>
        {% if userInfo.Score > 0 %}
            <input type="checkbox" id="selectScore">
        {% endif %}
    </label>
</div>
<div class="goods-total flex-wrap" >
    <div class="flex-1">余额 {{userInfo.Money}}</div>
</div>
<section class="bottom-fixed list-bottom-fixed">
    <div class="flex-wrap">
        <div class="flex-1">
            实际需支付 <span id="total">{{product.UnitPrice * num}}</span>元
        </div>
        <a href="javascript:void(0)" id="submit" class="ctrl-btn" style="margin: 0;">确认支付</a>
    </div>
</section>

{% include '../script.html'%}
<script>
    var total = $('#total');
    var selectScore = $('#selectScore');
    selectScore.on('change' ,function (){
        var _this = $(this);

        var change = {{userInfo.Score / 100}};
        if(_this.prop('checked')){
            if(change > {{product.UnitPrice * num}} || change == {{product.UnitPrice * num}}){
                total.text(0);
            } else {
                total.text({{product.UnitPrice * num}} - change );
            }
        } else {
            total.text({{product.UnitPrice * num}});
        }
    });
    $('#submit').on('click' ,function (){
        var OrderType = {{type}};
        var ItemId = {{product.Id}};
        var IsAutoOrder = false;
        var OrderCount = {{num}};
        var PayDatas = [];
        PayDatas.push({
            PayType: 1,
            Money: {{product.UnitPrice * num}},
            Score: 0
        });
        $.ajax({
            url:'submitOrder',
            type:'post',
            data:{
                OrderType:OrderType,
                ItemId:ItemId,
                IsAutoOrder:IsAutoOrder,
                OrderCount:OrderCount,
                PayDatas:PayDatas
            }
        }).done(function(data){
            if(data['IsSuccess']){
                location.href = '/';
                $.alert('购买成功');
            } else {
                $.alert('购买失败');
            }
        });
    });
</script>
</body>
{% endblock%}